package cn.sy.util;


import cn.sy.exception.GlobalCustomException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

public class EmailUtil {

    private static JavaMailSender sender
            = ApplicationContextUtil.get(JavaMailSender.class);
    private static String from = "1604772907@qq.com";

    /**
     *
     * @param to 邮件接收者
     * @param subject 邮件主题
     * @param content 邮件内容
     */
    public static void sendSimpleEmail(String to, String subject, String content) {
        try {
            MimeMessage message = sender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);
            sender.send(message);
        } catch (MessagingException e) {
            throw new GlobalCustomException("发送邮件异常!");
        }
    }
}
